package movimentimdbsender;


import it.vgnf.ejb3.beans.MovimentoMonetario;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author valerio
 */
public class MovimentiMonetariMDBSender {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws NamingException, JMSException {
        
        Properties properties = new Properties();
        properties.setProperty("org.omg.CORBA.ORBInitialPort", "46081");
        Context ctx = new InitialContext(properties);
        
        //recuperare connection factory
        ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("jms/MovimentiMonetariConnectionFactory");
          
        //recuperare coda jms/RisorseUmaneQueue
        Queue que = (Queue) ctx.lookup("jms/MovimentiMonetariQueue");
        
        //su connection factory creare una connection col metodo create connection
        Connection conn = (Connection) connectionFactory.createConnection();
        
        //da connection crearmi una session, con create session
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        
        //da session creare il producer sulla coda col metodo create producer
        MessageProducer messageProducer = session.createProducer(que);
        
        //da session creare l'object messagge, create object messagge, impostare il body del messaggio, setobject
        ObjectMessage objectMessage = session.createObjectMessage();
        ArrayList<MovimentoMonetario> movimenti = creaMovimentiMonetariTest();
        objectMessage.setObject(movimenti);
        
        objectMessage.setStringProperty("Task", "insert");

        messageProducer.send(objectMessage);
        
        conn.close();
        
    }
    
   private static ArrayList<MovimentoMonetario> creaMovimentiMonetariTest() {
        ArrayList<MovimentoMonetario> movimenti = new ArrayList<MovimentoMonetario>();

        for (int i = 0; i < 10; i++) {
            MovimentoMonetario movimento = new MovimentoMonetario();
            movimento.setDataMovimento(new Date(System.currentTimeMillis()));
            movimento.setDescrizioneMovimento("descr_" + i);
            movimento.setNote("note_" + i);
            movimento.setImporto(10.569 * i);
            movimenti.add(movimento);
        }
        return movimenti;
    }
}
